/**
 * 管理员页面处理脚本
 */
$(function () {
    /**
     * 修改用户按钮
     * 表内此行的数据放入模态框中
     * */
    $(".btn_changeUser").on('click',function () {
        var user_uid = $(this).parent().parent().children(":first").text();
        var user_name = $(this).parent().parent().children().eq(1).text();
        var user_pwd = $(this).parent().parent().children().eq(2).text();
        var user_pid = $(this).parent().parent().children().eq(3).text();
        $('#changeUser_id').text(user_uid);
        $('#changeUser_name').val(user_name);
        $('#changeUser_pwd').val(user_pwd);
        //oldPid用于后台判断当前管理员是否只有一个，否则不可以将自己变为普通用户
        $('#changeUser_pid').val(user_pid);
        $('#changeUser_oldPid').val(user_pid);
    });

    /**
     * 删除用户按钮
     * 表内此行的数据放入模态框中
     * */
    $(".btn_deleteUser").on('click',function () {
        var user_uid = $(this).parent().parent().children(":first").text();
        $('#deleteUser_id').text(user_uid);
    });

    /**
     * 修改权限按钮
     * 表内此行的数据放入模态框中
     * */
    $(".btn_changePermission").on('click',function () {
        var pid = $(this).parent().parent().children(":first").text();
        var pname = $(this).parent().parent().children().eq(1).text();
        var pdesc = $(this).parent().parent().children().eq(2).text();
        $('#changePermission_id').text(pid);
        $('#changePermission_pname').val(pname);
        $('#changePermission_pdesc').val(pdesc);
    });

    /**
     * 删除权限按钮
     * 表内此行的数据放入模态框中
     * */
    $(".btn_deletePermission").on('click',function () {
        var pid = $(this).parent().parent().children(":first").text();
        $('#deletePermission_id').text(pid);
    });

    /**
     * 修改视频按钮
     */
    $(".btn_changeVideo").on('click',function () {
        var vid = $(this).parent().parent().children(":first").text();
        var vtitle = $(this).parent().parent().children().eq(1).text();
        var vdesc = $(this).parent().parent().children().eq(4).text();
        var vprice = $(this).parent().parent().children().eq(6).text();
        var vtypeid = $(this).parent().parent().children().eq(7).text();
        $('#changeVideo_id').text(vid);
        $('#changeVideo_hidden_id').val(vid);
        $('#changeVideo_vtitle').val(vtitle);
        $('#changeVideo_vdesc').val(vdesc);
        $('#changeVideo_vprice').val(vprice);
        $('#changeVideo_vtypeid').val(vtypeid);
    });

    /**
     * 删除视频按钮
     */
    $(".btn_deleteVideo").on('click',function () {
        var vid = $(this).parent().parent().children(":first").text();
        $('#deleteVideo_id').text(vid);
    });

    /**
     * 删除博客按钮
     */
    $(".btn_deleteBlog").on('click',function () {
        var bid = $(this).parent().parent().children(":first").text();
        $('#deleteBlog_id').text(bid);
    });

    /**
     * 删除评论按钮
     */
    $(".btn_deleteComment").on('click',function () {
        var cmid = $(this).parent().parent().children(":first").text();
        $('#deleteComment_id').text(cmid);
    });

    /**
     * 删除订单按钮
     */
    $(".btn_deleteOrder").on('click',function () {
        var oid = $(this).parent().parent().children(":first").text();
        $('#deleteOrder_id').text(oid);
    });

    /**
     * 模态框模块
     */
    //定义用户名密码正则
    var patternUsername = /^[A-Za-z0-9_\u4e00-\u9fa5]{2,10}$/;
    var patternPassword = /^[A-Za-z0-9_]{5,12}$/;
    //表单提交前的验证参数
    var usernameInputOK = false;
    var passwordInputOK = false;

    /**
     * 添加用户模态框提交
     */
    $('#btn_addUser_save').on('click',function () {
        var addUser_name = $("#addUser_name").val().trim();
        var addUser_pwd = $("#addUser_pwd").val().trim();
        var addUser_pid = $("#addUser_pid").val().trim();
        console.log("表单信息："+addUser_name+","+addUser_pwd+","+addUser_pid);
        // 调用用户输入验证方法
        var addUser_nameInput = $("#addUser_name");
        var addUser_pwdInput = $("#addUser_pwd");
        userInputCheck(addUser_nameInput, addUser_pwdInput, addUser_name, addUser_pwd);
        if (!(usernameInputOK && passwordInputOK)) {
            console.log("表单验证未通过");
            if (!usernameInputOK){
                $("#addUser_name").focus().select();
            }else{
                $("#addUser_pwd").focus().select();
            }
            return false;
        }
        var form_addUser = {
            username: addUser_name,
            password: addUser_pwd,
            p_id: addUser_pid
        }
        $.ajax({
            url: 'account/addUser',
            type: "POST",
            data: form_addUser,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 修改用户模态框提交
     */
    $('#btn_changeUser_save').on('click',function () {
        var changeUser_id = $("#changeUser_id").text().trim();
        var changeUser_name = $("#changeUser_name").val().trim();
        var changeUser_pwd = $("#changeUser_pwd").val().trim();
        var changeUser_pid = $("#changeUser_pid").val().trim();
        var changeUser_oldPid = $("#changeUser_oldPid").val().trim();
        console.log("表单信息："+changeUser_id+","+changeUser_name+","+changeUser_pwd+","+changeUser_pid+","+changeUser_oldPid);
        // 调用用户输入验证方法
        var changeUser_nameInput = $("#changeUser_name");
        var changeUser_pwdInput = $("#changeUser_pwd");
        userInputCheck(changeUser_nameInput, changeUser_pwdInput, changeUser_name, changeUser_pwd);
        if (!(usernameInputOK && passwordInputOK)) {
            console.log("表单验证未通过");
            if (!usernameInputOK){
                $("#changeUser_name").focus().select();
            }else{
                $("#changeUser_pwd").focus().select();
            }
            return false;
        }
        var form_changeUser = {
            id: changeUser_id,
            username: changeUser_name,
            password: changeUser_pwd,
            p_id: changeUser_pid,
            oldP_id: changeUser_oldPid
        }
        $.ajax({
            url: 'account/updateUser',
            type: "POST",
            data: form_changeUser,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除用户模态框提交
     */
    $('#btn_deleteUser_save').on('click',function () {
        var deleteUser_id = $("#deleteUser_id").text().trim();
        console.log("提交信息:"+deleteUser_id);
        var form_deleteUser = {
            id: deleteUser_id
        }
        $.ajax({
            url: 'account/deleteUser',
            type: "POST",
            data: form_deleteUser,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 添加权限模态框提交
     */
    $('#btn_addPermission_save').on('click',function () {
        var addPermission_pname = $("#addPermission_pname").val().trim();
        var addPermission_pdesc = $("#addPermission_pdesc").val().trim();
        console.log("表单信息："+addPermission_pname+","+addPermission_pdesc);
        if (addPermission_pname == "") {
            $("#addPermission_pname").attr('placeholder',"权限名不能为空");
            $("#addPermission_pname").focus().select();
            return false;
        }
        if (addPermission_pdesc == "") {
            $("#addPermission_pdesc").attr('placeholder',"权限描述不能为空");
            $("#addPermission_pdesc").focus().select();
            return false;
        }
        var form_addPermission = {
            pname: addPermission_pname,
            pdesc: addPermission_pdesc
        }
        $.ajax({
            url: 'permission/addPermission',
            type: "POST",
            data: form_addPermission,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 修改权限模态框提交
     */
    $('#btn_changePermission_save').on('click',function () {
        var changePermission_id = $("#changePermission_id").text().trim();
        var changePermission_pname = $("#changePermission_pname").val().trim();
        var changePermission_pdesc = $("#changePermission_pdesc").val().trim();
        console.log("表单信息："+changePermission_id+","+changePermission_pname+","+changePermission_pdesc);
        if (changePermission_pname == "") {
            $("#changePermission_pname").attr('placeholder',"权限名不能为空");
            $("#changePermission_pname").focus().select();
            return false;
        }
        if (changePermission_pdesc == "") {
            $("#changePermission_pdesc").attr('placeholder',"权限描述不能为空");
            $("#changePermission_pdesc").focus().select();
            return false;
        }
        var form_changePermission = {
            id: changePermission_id,
            pname: changePermission_pname,
            pdesc: changePermission_pdesc,
        }
        $.ajax({
            url: 'permission/updatePermission',
            type: "POST",
            data: form_changePermission,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除权限模态框提交
     */
    $('#btn_deletePermission_save').on('click',function () {
        var deletePermission_id = $("#deletePermission_id").text().trim();
        console.log("提交信息:"+deletePermission_id);
        var form_deletePermission = {
            id: deletePermission_id
        }
        $.ajax({
            url: 'permission/deletePermission',
            type: "POST",
            data: form_deletePermission,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 修改管理员密码提交
     */
    $('#btn_changeAdmin_save').on('click',function () {
        var changeAdmin_oldPwd = $("#changeAdmin_oldPwd").val().trim();
        var changeAdmin_newPwd = $("#changeAdmin_newPwd").val().trim();
        var changeAdmin_repeatPwd = $("#changeAdmin_repeatPwd").val().trim();
        console.log("表单信息："+changeAdmin_oldPwd+","+changeAdmin_newPwd+","+changeAdmin_repeatPwd);
        if (changeAdmin_oldPwd == "") {
            $("#changeAdmin_oldPwd").attr('placeholder',"原密码不能为空");
            $("#changeAdmin_oldPwd").focus().select();
            return false;
        }
        if (changeAdmin_newPwd == "") {
            $("#changeAdmin_newPwd").attr('placeholder',"新密码不能为空");
            $("#changeAdmin_newPwd").focus().select();
            return false;
        }
        if (changeAdmin_repeatPwd == "") {
            $("#changeAdmin_repeatPwd").attr('placeholder',"新密码不能为空");
            $("#changeAdmin_repeatPwd").focus().select();
            return false;
        }
        if (changeAdmin_newPwd != changeAdmin_repeatPwd){
            $("#changeAdmin_newPwd").val("");
            $("#changeAdmin_repeatPwd").val("");
            $("#changeAdmin_newPwd").attr('placeholder',"密码不一致");
            $("#changeAdmin_repeatPwd").attr('placeholder',"密码不一致");
            $("#changeAdmin_newPwd").focus().select();
            return false;
        }
        var form_changeAdminPwd = {
            oldPwd: changeAdmin_oldPwd,
            newPwd: changeAdmin_repeatPwd
        }
        $.ajax({
            url: 'account/changeAdminPwd',
            type: "POST",
            data: form_changeAdminPwd,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除视频模态框提交
     */
    $('#btn_deleteVideo_save').on('click',function () {
        var deleteVideo_id = $("#deleteVideo_id").text().trim();
        console.log("提交信息:"+deleteVideo_id);
        var form_deleteVideo = {
            id: deleteVideo_id
        }
        $.ajax({
            url: 'deleteVideo',
            type: "POST",
            data: form_deleteVideo,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除博客模态框提交
     */
    $('#btn_deleteBlog_save').on('click',function () {
        var deleteBlog_id = $("#deleteBlog_id").text().trim();
        console.log("提交信息:"+deleteBlog_id);
        var form_deleteBlog = {
            id: deleteBlog_id
        }
        $.ajax({
            url: 'deleteBlog',
            type: "POST",
            data: form_deleteBlog,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除评论模态框提交
     */
    $('#btn_deleteComment_save').on('click',function () {
        var deleteComment_id = $("#deleteComment_id").text().trim();
        console.log("提交信息:"+deleteComment_id);
        var form_deleteComment = {
            id: deleteComment_id
        }
        $.ajax({
            url: 'deleteComment',
            type: "POST",
            data: form_deleteComment,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });

    /**
     * 删除订单模态框提交
     */
    $('#btn_deleteOrder_save').on('click',function () {
        var deleteOrder_id = $("#deleteOrder_id").text().trim();
        console.log("提交信息:"+deleteOrder_id);
        var form_deleteOrder = {
            id: deleteOrder_id
        }
        $.ajax({
            url: 'deleteOrder',
            type: "POST",
            data: form_deleteOrder,
            dataType: 'json',
            success: function (msg) {
                console.log(msg);
                if (msg.resCode == 1) {
                    alert(msg.resultMsg);
                    window.location.href = "indexAdmin";
                } else {
                    alert(msg.resultMsg);
                }
            },
            error: function (e) {
                alert("错误：" + e);
            }
        });
    });


    /**
     * 输入验证
     * */
    function userInputCheck(nameInput, pwdInput, username, password){
        if (username == "") {
            nameInput.attr('placeholder',"用户名不能为空");
            nameInput.focus().select();
            usernameInputOK = false;
        }else if (patternUsername.test(username)){
            usernameInputOK = true;
        }else{
            nameInput.val("");
            nameInput.attr('placeholder',"用户名格式错误");
            usernameInputOK = false;
        }

        if (password == "") {
            pwdInput.attr('placeholder',"密码不能为空");
            pwdInput.focus().select();
            passwordInputOK = false;
        }else if (patternPassword.test(password)){
            passwordInputOK = true;
        }else{
            pwdInput.val("");
            pwdInput.attr('placeholder',"密码格式错误");
            passwordInputOK = false;
        }
    }
});